﻿Imports TreeCreeper01Interfaces

Friend Class cSpatialAssistant

    Private pAbstractDataAgent As cAbstractDataAgent
    Private pAbstractReader As cAbstractReader

    Protected pColumnIndexForXcoordinate As Integer
    Protected pColumnIndexForYcoordinate As Integer
    Protected pColumnIndexForCanBeRandomized As Integer

    Public Sub New(ByVal AbstractDataAgent As cAbstractDataAgent)
        pAbstractDataAgent = AbstractDataAgent
        pAbstractReader = AbstractDataAgent.AbstractReader
        InferMetaInfo()
    End Sub

    Private Sub InferMetaInfo()

        'Here is the logic of the data structure for spatial data sets
        With pAbstractReader

            pAbstractDataAgent.TaxonomicDataColumnScanEndIndex = .EstimatedNumberOfTaxonomicColumns
            If .DoesHaveNumericFirstColumn Then
                pAbstractDataAgent.TaxonomicDataColumnScanStartIndex = 2
                pAbstractDataAgent.TaxonomicDataColumnScanEndIndex = pAbstractDataAgent.TaxonomicDataColumnScanEndIndex + 1
            Else
                pAbstractDataAgent.TaxonomicDataColumnScanStartIndex = 1
            End If

            pColumnIndexForXcoordinate = pAbstractDataAgent.TaxonomicDataColumnScanEndIndex + 1
            pColumnIndexForYcoordinate = pColumnIndexForXcoordinate + 1

            If .DoesHaveBooleanNumericLastColumn Then
                pColumnIndexForCanBeRandomized = pColumnIndexForYcoordinate + 1
            Else
                pColumnIndexForCanBeRandomized = -1
            End If

        End With


    End Sub


    Public Sub PopulateStudySpatial(ByVal Study As IStudySpatial)
        pAbstractDataAgent.PopulateTaxonomicTree(Study.TaxonomicTree)
        PopulatePlantList(Study.TaxonomicTree.AllSpecies, Study.PlantList)
    End Sub


    Private Sub PopulatePlantList(ByVal TaxonomicSpeciesCollection As ITaxonomicSpeciesCollection, ByVal PlantList As IPlantList)

        Dim GroupingInteger As Integer
        Dim SpeciesUniqueKey As String
        Dim Species As ITaxonomicSpecies
        Dim XCoordinate As Double
        Dim YCoordinate As Double
        Dim CanBeRandomized As Boolean
        Dim RowIndex As Integer
        Dim NumberOfRows As Integer
        Dim BooleanHolder As String

        NumberOfRows = pAbstractReader.NumberOfRows

        For RowIndex = IndexOfFirstDataRow To NumberOfRows

            GroupingInteger = pAbstractReader.NumericFirstColumnValue(RowIndex)
            SpeciesUniqueKey = pAbstractDataAgent.GenerateUniqueKeyForSpecies(RowIndex)
            Species = TaxonomicSpeciesCollection.TaxonomicSpeciesByUniqueKey(SpeciesUniqueKey)
            XCoordinate = pAbstractReader.ReadDoubleValue(RowIndex, pColumnIndexForXcoordinate)
            YCoordinate = pAbstractReader.ReadDoubleValue(RowIndex, pColumnIndexForYcoordinate)

            If pColumnIndexForCanBeRandomized = -1 Then
                CanBeRandomized = True ' if randomization column does not exist, everything can be randomized by default
            Else
                BooleanHolder = pAbstractReader.GetStringValue(RowIndex, pColumnIndexForCanBeRandomized)
                CanBeRandomized = CBool(BooleanHolder)
            End If

            AddPlantToPlantList(PlantList, GroupingInteger, Species, XCoordinate, YCoordinate, CanBeRandomized)

        Next

        PlantList.CalculateAndCacheAllSpatialDistancesAndShortIds()

    End Sub


End Class
